<template>
  <div class="com-container">
      <div class="title" :style="comStyle">
          <span>{{' ▎ ' + showTitle }}</span>
          <span class="iconfont title-icon" :style="comStyle" @click="showChoice = !showChoice">&#xe6eb;</span>
          <div class="select-con" v-show="showChoice" :style="marginStyle">
              <div class="select-item" v-for="item in selectTypes" :key="item.key" @click="handleSelect(item.key)">{{item.text}}</div>
          </div>
      </div>
    <div class="com-chart" ref="trend_ref"></div>
  </div>
</template>
<script>
import { mapState } from 'vuex'
import { getThemeValue } from '@/utils/theme_utils'

export default {
  data() {
    return {
      chartInstance: null,
      allData: null, // 服务器返回的数据
      currentPage: 1, // 当前显示的页数
      totalPage: 0, // 一共有多少页
      showChoice: false, //是否显示可选框
      choiceType: 'map',  //选择显示的图表
      titleFontSize: 0  //指明标题的字体大小
    };
  },
  created() {
    this.$socket.registerCallBack("trendData", this.getData)
  },
  computed: {
      selectTypes() {
          if(!this.allData) {
              return []
          }else {
              return this.allData.type
          }
      },
      showTitle() {
           if(!this.allData) {
               return ''
           }else {
               return this.allData[this.choiceType].title
           }
      },
      // 设置给标题的样式
      comStyle() {
          return {
              fontSize: this.titleFontSize + 'px',
              color: getThemeValue(this.theme).titleColor
          }
      },
      marginStyle() {
          return {
              marginLeft: this.titleFontSize + 'px'
          }
      },
       ...mapState(['theme'])
  },
  mounted() {
    this.initChart();
    // this.getData();
    this.$socket.send({
      action: 'getData',
      socketType: 'trendData',
      chartName: 'trend',
      value: ''
    });
    window.addEventListener("resize", this.sceenAdapter);
    this.sceenAdapter();
  },
  destroyed() {
    window.removeEventListener("resize", this.sceenAdapter);
     this.$socket.unRegisterCallBack("trendData")
  },
  methods: {
    //初始化echartInstance对象
    initChart() {
      this.chartInstance = this.$echarts.init(this.$refs.trend_ref, this.theme);
      //对图表初始化配置的控制
      const initOption = {
        grid: {
          top: "35%",
          left: "3%",
          right: "4%",
          bottom: "1%",
          containLabel: true,
        },
        xAxis: {
          type: "category",
          boundaryGap: false,
        },
        yAxis: {
          type: "value",
        },
        tooltip: {
          trigger: "axis",
        },
        legend: {
          left: 20,
          top: "15%",
          icon: "circle",
        },
        series: [
          {
            type: "bar",
            label: {
              show: true,
              position: "right",
              textStyle: {
                color: "white",
              },
            },
            itemStyle: {
              //颜色渐变
              color: new this.$echarts.graphic.LinearGradient(0, 0, 1, 0, [
                {
                  offset: 0,
                  color: "#5052EE",
                },
                {
                  offset: 1,
                  color: "#AB6EE5",
                },
              ]),
            },
          },
        ],
      };
      this.chartInstance.setOption(initOption);
    },
    // 获取服务器的数据
    async getData(res) {
      // const { data: res } = await this.$http.get("trend");
      console.log(res);
      this.allData = res;
      this.updateChart();
    },
    // 更新图表
    updateChart() {
        // 半透明的颜色值
      const colorArr1 = [
        'rgba(11, 168, 44, 0.5)',
        'rgba(44, 110, 255, 0.5)',
        'rgba(22, 242, 217, 0.5)',
        'rgba(254, 33, 30, 0.5)',
        'rgba(250, 105, 0, 0.5)'
      ]
      // 全透明的颜色值
      const colorArr2 = [
        'rgba(11, 168, 44, 0)',
        'rgba(44, 110, 255, 0)',
        'rgba(22, 242, 217, 0)',
        'rgba(254, 33, 30, 0)',
        'rgba(250, 105, 0, 0)'
      ]
      //处理数据
      //x轴的数据
      const timeArr = this.allData.common.month;
      //y轴的数据
      const valueArr = this.allData[this.choiceType].data;
      const seriesArr = valueArr.map((item, index) => {
        return {
          name: item.name,
          type: "line",
          data: item.data,
          stack: this.choiceType,
          areaStyle: {
            color: new this.$echarts.graphic.LinearGradient(0, 0, 0, 1, [
                {
                    offset: 0,
                    color: colorArr1[index]
                },
                {
                    offset: 1,
                    color: colorArr2[index]
                }
            ]),
          },
        };
      });
      //图例的数据
      const legendArr = valueArr.map((item) => {
        return item.name;
      });
      const dataOption = {
        xAxis: {
          data: timeArr,
        },
        legend: {
          data: legendArr,
        },
        series: seriesArr,
      };
      this.chartInstance.setOption(dataOption);
    },
    handleSelect(selectType) {
        this.choiceType = selectType
        this.updateChart()
        this.showChoice = false
    },
    //适应屏幕大小
    sceenAdapter() {
    this.titleFontSize = (this.$refs.trend_ref.offsetWidth / 100) * 3.6;
      //和分辨率相关的文字大小
      const adapterOption = {
        legend: {
            itemWidth: this.titleFontSize,
            itemHeight: this.titleFontSize,
            itemGap: this.titleFontSize,
            textStyle: {
                fontSize: this.titleFontSize / 2
            }
        }
      };
      this.chartInstance.setOption(adapterOption);

      this.chartInstance.resize();
    },
  },
   watch: {
    theme() {
      // console.log('主题切换');
      this.chartInstance.dispose() //销毁当前的图表
      this.initChart()
      this.sceenAdapter()
      this.updateChart()

    }
  }
};
</script>

<style lang='less' scoped>
.title {
    position: absolute;
    left: 20px;
    top: 20px;
    z-index: 10;
    color: #fff;
    .title-icon {
        margin-left: 10px;
        cursor: pointer;
    }
    .select-item {
        cursor: pointer;
    }
    .select-con {
        background-color: #222733;
    }
}
</style>